class Solution {//leetcode134——加油站
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n=gas.size();
        int step=0;
        for(int i=0;i<n;i++){
            int rest=0;
            while(step<n){
                int pos=(i+step)%n;
                rest+=gas[pos]-cost[pos];
                if(rest<0) break;
                step++;
            }
            if(step==n&&rest>=0) return i;
            i+=step;
            step=0;
        }
        return -1;
    }
};